export const h = (tag, props = null, children = null) => {
  if (props === null) {
    let txt = document.createTextNode(tag);
    txt._render = (v) => (txt.nodeValue = v);
    return txt;
  }
  if (typeof tag === "function") {
    return cpt(tag, props, children);
  }
  return createDom(tag, props, children);
};
export const u = (node, props, children) => {
  // dom 节点维护
  if (typeof node === "object") {
    if (typeof props == "object") {
      for (let p in props) {
        node[p] = props[p];
      }
      if (children) {
        replaceChildren(node, children);
      }
    } else {
      node.innerHTML = props;
    }
    console.warn("nodeUpdate",node,props)
    return node;
  } 
};
export const replaceChildren = (node, children) => {
  node.innerHtml = "";
  if (Array.isArray(children)) {
    children.forEach((s) => {
      node.append(s);
    });
  } else {
    node.append(children);
  }
};
const createDom = (tag, props, children) => {
  let dom = document.createElement(tag);

  if (typeof props === "object" && !Array.isArray(props)) {
    let emit;
    for (let s in props) {
      if (s == "vmodel") continue;
      if (s.startsWith("@")) {
        emit = dom._emit || (dom._emit = {});
        emit[s] = props[s];
        // console.warn("emit", tag, s, emit);
        continue;
      }
      if (s.startsWith("on")) {
        let key = s.slice(2).toLowerCase().split(".");
        if (key[1] == "once") {
          console.warn("dddd", s);
          dom.addEventListener(key[0], props[s], { once: true });
        } else {
          dom.addEventListener(key[0], props[s]);
        }
      } else if (s == "className") {
        dom.className = props[s];
      } else if (s == "style") {
        let style = dom.style;
        let stl = props[s];
        for (let k in stl) {
          style[k] = stl[k];
        }
      } else {
        dom.setAttribute(s, props[s]);
      }
    }
  } else {
    if (children == null && !children) {
      children = props;
    } else {
      patch(dom, props);
    }
  }
  if (children !== null) {
    if (Array.isArray(children)) {
      children.forEach((c) => {
        patch(dom, c);
      });
    } else {
      patch(dom, children);
    }
  }
  return dom;
};
const cpt = (component, props, children = null) => {
  return component(props, children);
};
export const patch = (root, node, props = null, children = null) => {
  if (typeof node === "object") {
    root.append(node);
  } else if (typeof node === "function") {
    root.append(node(props, children));
  } else {
    // console.warn(root,node,props,children)
    // root.append(node)
    root.insertAdjacentHTML("beforeend", node);
    // root.innerHTML=children;
  }
};
export const Qinput = (props, data) => {
  let input;
  const { vmodel } = props;
  props.value = data[vmodel];
  props.onInput = (e) => {
    data[vmodel] = e.target.value;
  };
  return (input = h("input", { ...props }));
};

export const useObserver = () => {
  const subscriber = [];
  const send = (code, ...data) => {
    subscriber
      .filter((s) => s.code == code)
      .forEach((s) => {
        s.fn(...data);
      });
  };
  const listen = (code, fn) => {
    subscriber.push({ code, fn });
  };
  return {
    send,
    listen,
  };
};
let seq = 0;
export const dayseq = () => {
  let date = new Date();
  let year = date.getFullYear() % 100; //8
  let mouth = date.getMonth(); //4
  let day = date.getDate(); //5
  seq++;
  return (((year << 9) | (mouth << 5) | day) << 16) | seq;
};
